package com.qingzhou.web.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.StrUtil;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.solon.service.impl.ServiceImpl;
import com.qingzhou.web.repository.dao.HospitalDistrictDao;
import com.qingzhou.web.repository.entity.CheckRecordsEntity;
import com.qingzhou.web.repository.entity.HospitalDistrictEntity;
import com.qingzhou.web.repository.mapper.HospitalDistrictMapper;
import com.qingzhou.web.service.HospitalDistrictService;
import com.qingzhou.web.service.impl.params.request.HospitalDistrictAddForm;
import com.qingzhou.web.service.impl.params.request.HospitalDistrictDeleteForm;
import com.qingzhou.web.service.impl.params.request.HospitalDistrictDetailForm;
import com.qingzhou.web.service.impl.params.request.HospitalDistrictEditForm;
import com.qingzhou.web.service.impl.params.vo.HospitalDistrictVo;
import org.noear.solon.annotation.Component;
import org.noear.solon.annotation.Inject;

@Component
public class HospitalDistrictServiceImpl extends ServiceImpl<HospitalDistrictMapper, HospitalDistrictEntity> implements HospitalDistrictService {
    @Inject
    HospitalDistrictDao hospitalDistrictDao;
    @Override
    public int add(HospitalDistrictAddForm form) {
        Assert.isTrue(StrUtil.isNotEmpty(form.getHospitalSite()),"医院字典码不允许为空");
        Assert.isTrue(StrUtil.isNotEmpty(form.getHospitalSiteName()),"医院名称不允许为空");
        Assert.isTrue(StrUtil.isNotEmpty(form.getDistrictCode()),"院区编码不允许为空");
        Assert.isTrue(StrUtil.isNotEmpty(form.getDistrictName()),"院区名称不允许为空");

        HospitalDistrictEntity entity = BeanUtil.copyProperties(form, HospitalDistrictEntity.class);
        long l = hospitalDistrictDao.getMapper().selectCountByQuery(QueryWrapper.create().eq("district_code", entity.getDistrictCode()));
        Assert.isTrue(l==0,"失败：已存在相同的院区编码");
        return hospitalDistrictDao.add(entity);
    }

    @Override
    public int delete(HospitalDistrictDeleteForm form) {
        Assert.isTrue(ObjUtil.isNotNull(form.getId()),"请选择数据");
        HospitalDistrictEntity entity = hospitalDistrictDao.getById(form.getId());
        Assert.isTrue(ObjUtil.isNotNull(entity),"数据错误，请刷新后重新选择数据");

        return hospitalDistrictDao.delete(form.getId());
    }

    @Override
    public HospitalDistrictVo detail(HospitalDistrictDetailForm form) {
        Assert.isTrue(ObjUtil.isNotNull(form.getId()),"请选择数据");
        HospitalDistrictEntity entity = hospitalDistrictDao.getById(form.getId());
        Assert.isTrue(ObjUtil.isNotNull(entity),"数据错误，请刷新后重新选择数据");

        return BeanUtil.copyProperties(entity,HospitalDistrictVo.class);
    }

    @Override
    public int edit(HospitalDistrictEditForm form) {
        Assert.isTrue(ObjUtil.isNotNull(form.getId()),"请选择数据");
        HospitalDistrictEntity entity = hospitalDistrictDao.getById(form.getId());
        Assert.isTrue(ObjUtil.isNotNull(entity),"数据错误，请刷新后重新选择数据");

        BeanUtil.copyProperties(form,entity);
        return hospitalDistrictDao.update(entity);
    }
}
